/**
 * @file arc.c
 * @brief Fichier source du module arc.
 *
 * @author Rachid Delory
 */

#include "arc.h"

void init_Arc(Element a)
{ 
  if(a != NULL){
    ((Arc*)a)->valeur = 0;
    ((Arc*)a)->source = NULL;
    ((Arc*)a)->destination = NULL;
  }
}

void initCopie_Arc(Element *a1, const Element a2)
{
  if(((Arc*)*a1) != NULL && ((Arc*)a2) != NULL)
    {
      init_Arc(*a1);
      ((Arc*)*a1)->valeur = ((Arc*)a2)->valeur;
      ((Arc*)*a1)->source = ((Arc*)a2)->source;
      ((Arc*)*a1)->destination = ((Arc*)a2)->destination;
    }
}

void creerCopie_Arc(Element *a1, const Element a2)
{
  (*((Arc **)a1)) = creer_Arc(((Arc *) a2)->valeur, ((Arc *) a2)->source, ((Arc *) a2)->destination);
}


Arc * creer_Arc(const int valeur, Element source, Element destination)
{
  Arc * res;
  res = (Arc*) malloc(sizeof(Arc));
  init_Arc(res);
  ((Arc*)res)->valeur = valeur;
  ((Arc*)res)->source = (Noeud*)source;
  ((Arc*)res)->destination = (Noeud*)destination;
  return ((Arc*)res);
}

void testament_Arc(Element a)
{
  if(a != NULL)
    {
      ((Arc*)a)->valeur = 0;
      ((Arc*)a)->source = NULL;
      ((Arc*)a)->destination = NULL;
    }
}

void detruire_Arc(Element *a)
{
  if(a != NULL)
    {
      Arc ** a1 = (Arc **)a;
      testament_Arc((*a1));
      free(*a1);
      (*a) = NULL;
    }
}

void afficher_Arc(const Element a)
{
  if(a != NULL)
    {
      printf("valeur : %d\n", ((Arc*)a)->valeur);
      printf("source : %s\n", ((Arc*)a)->source->nom);
      printf("destination : %s\n", ((Arc*)a)->destination->nom);
    }
}

int comparer_Arc(const Element a1, const Element a2)
{
  if(((Arc*)a1) != NULL && ((Arc*)a2) != NULL)
    {
      int resSour, resDest;
      /*
      if(((Arc*)a1)->valeur != ((Arc*)a1)->valeur)
	return 1;
      */
      resSour = comparer_Noeud((Noeud*)((Arc*)a1)->source, (Noeud*)((Arc*)a2)->source);
      resDest = comparer_Noeud(((Arc*)a1)->destination, ((Arc*)a2)->destination);
      if(resSour == 1 && resDest == 1)
	return 3;	
      if(resSour != 1 && resDest == 1)
	return 2;
      if(resSour == 1 && resDest != 1)
	return 1;
    }
  return 0;
}
  /* 3: source et dest identique; 2: même dest; 1: même source; 0 : source et dest différentes */ 
